from flask import Flask, render_template, request,escape
from geo import geocode
from walk import walking
import bu

#request
def log_request(req:'flask_request',res:str) -> None:
    with open('vsearch.log','a') as log:
        print(req,res,file=log)

#vsearch
def vsearchletters(phrase: str, letters: str) -> set:
    return set(letters).intersection(set(phrase))



高德api_key = "ddcdad5238b943e23a92493fc7541ea8"


app = Flask(__name__)


@app.route('/vsearch', methods=['POST'])
def do_search() -> 'html':
    color01 = request.form['color01']
    color02 = request.form['color02']
    start_address=request.form['start-walking']
    出发点=geocode(高德api_key,start_address)
    end_address=request.form['end-walking']
    目的地=geocode(高德api_key,end_address)
    步行规划=walking(高德api_key,出发点,目的地)
    print(步行规划)
    title = '查询结果'
    df = bu.direction_walking(高德api_key, start_address, end_address)
    results = df.loc[:, ['instruction', 'distance', 'duration']]
    results.columns = ['指引', '路程（米）', '时间（分钟）']
    total_walk = bu.total_walk(df)
    total_walk_time = bu.total_walk_time(df)
    route = results['指引'].tolist()
    title = '查询结果:'
    log_request(request,results)
    return render_template('results.html',
                           the_start_address=start_address,
                           the_end_address=end_address,
                           the_html_table=results.to_html(index=False),
                           the_total_walk=total_walk,
                           the_total_walk_time=total_walk_time,
                           the_color01=color01,
                           the_color02=color02,

                           )


#用户日志的管理页面
@app.route('/viewlog')
def view_log()->str:
	contents=[]
	with open("vsearch.log") as log:
		for line in log:
			contents.append([])
			for item in line.split("|"):
				contents[-1].append(escape(item))

	return render_template("log.html",the_html_table=contents)

@app.route('/')
def entry_page() -> 'html':
    title = '查询步行路径导航系统'
    return render_template('entry.html',
                           the_title=title,)


if __name__ == '__main__':
    app.run(debug=True)
